package com.heaven1949.tmall.common.web.exception;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Objects;

/**
 * ApiAssert
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiAssert {

    public static void equals(String errMsg, Object obj1, Object obj2) {
        if (!Objects.equals(obj1, obj2)) {
            failure(errMsg);
        }
    }

    public static void isTrue(String errMsg, boolean condition) {
        if (!condition) {
            failure(errMsg);
        }
    }

    public static void isFalse(String errMsg, boolean condition) {
        if (condition) {
            failure(errMsg);
        }
    }

    public static void isNullOrEmpty(String errMsg, Object condition) {
        if (ObjectUtils.isNotEmpty(condition)) {
            failure(errMsg);
        }
    }

    public static void notNullOrEmpty(String errMsg, Object condition) {
        if (ObjectUtils.isEmpty(condition)) {
            failure(errMsg);
        }
    }

    /**
     * <p>
     * 失败结果
     * </p>
     *
     * @param errMsg 异常错误信息
     */
    public static void failure(String errMsg) {
        throw new ValidException(errMsg);
    }

}
